package com.jit.app;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.jit.app.adapter.ReplayListAdapter;
import com.jit.app.model.AppInfoModel;
import com.jit.app.model.Constants;
import com.jit.app.util.DateFormatUtils;
import com.jit.app.util.JSONResult;
import java.util.List;
import java.util.Map;

public class ReplayListActivity extends AppCompatActivity {

    Map commentInfo;
    Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_replaylist);
        //把状态栏上的文字颜色这只为黑色
        MainActivity.setAndroidNativeLightStatusBar(this, true);

        //ToolBar
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("回复列表");
        this.setSupportActionBar(toolbar);

        //显示激活后退按钮
        this.getSupportActionBar().setHomeButtonEnabled(true);
        this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        commentInfo = (Map) getIntent().getSerializableExtra("commentInfo");

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch(msg.what){
                    case Constants.MessageWhat.REPLAYLIST_LOAD_SUCCESS:
                        JSONResult jsonResult2 = (JSONResult) msg.obj;
                        if(jsonResult2.getCode() == Constants.ResultCode.SUCCESS){
                            JSONArray result = (JSONArray) jsonResult2.getData().get("commentReplayList");
                            List<JSONObject> list = result.toJavaList(JSONObject.class);
                            replayListAdapter = new ReplayListAdapter(ReplayListActivity.this, R.layout.item_replaylist, list);
                            replayListView.setAdapter(replayListAdapter);
                        }
                        break;
                }
                return false;
            }
        });

        initView();

        initData();

    }


    ListView replayListView;
    ReplayListAdapter replayListAdapter;

    public ImageView headPicImageView;
    public TextView nickNameTextView;
    public RatingBar starsRatingBar;
    public TextView contentTextView;
    private TextView creationDateTextView;
    private TextView totalReplayTextView;
    ScrollView scrollView;
    private void initView() {

        scrollView = findViewById(R.id.scrollview);
        scrollView.smoothScrollTo(0, 0);

        headPicImageView = this.findViewById(R.id.iv_headpic);
        nickNameTextView = this.findViewById(R.id.tv_nickname);
        starsRatingBar = this.findViewById(R.id.rb_stars);
        contentTextView = this.findViewById(R.id.tv_content);
        creationDateTextView = this.findViewById(R.id.tv_creationdate);
        totalReplayTextView = this.findViewById(R.id.tv_replays);

        replayListView = this.findViewById(R.id.lv_replaylist);

    }

    private void initData(){
        String picPath = commentInfo.get("picPath").toString();
        if(picPath!=null && !picPath.equals("")){
            Glide.with(ReplayListActivity.this).load(Constants.HOST.FILE_HOST+picPath).into(headPicImageView);
        }
        nickNameTextView.setText(commentInfo.get("nickName").toString());
        starsRatingBar.setRating(Float.parseFloat(commentInfo.get("stars").toString()));
        contentTextView.setText(commentInfo.get("content").toString());
        creationDateTextView.setText(DateFormatUtils.toString((Long)commentInfo.get("creationDate"),"yyyy-MM-dd"));
        totalReplayTextView.setText((Integer) commentInfo.get("replayNum")+"");

        new AppInfoModel().loadReplayList(handler, (Integer)commentInfo.get("id"));
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.menu_replaylist,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.share:
                Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home://后退按钮的id
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
